var showAddWindow = function(node){
	var panel, wtitle;
	
	if (node[0].data.id == "root") {
		wtitle = "Add new Company";
		panel = createAddCompanyPanel();
	} else if (node[0].data.id.indexOf("company") != -1){
		wtitle = "Add new Division";
		panel = createAddDivisionPanel();
	} else if (node[0].data.id.indexOf("division") != -1){
		wtitle = "Add new Project";
		panel = createAddProjectPanel();
	} else if (node[0].data.id.indexOf("project") != -1){
		wtitle = "Add new Employee";
		panel = createAddEmployeePanel();
	} else if (node[0].data.id.indexOf("employee") != -1){
		alert("Can't get there!");
	}

	if (panel){
		var handleOK = function(){
			var valid = Ext.getCmp("addWindow").valid; 
				panel.submit({
					url: "/MyCompany/app/add",
					params: {
						parentNode : node[0].data.id
					},
					waitTitle: "Wait",
					waitMsg: "Performing operation..",
					success: function(form, action) {
						var json = Ext.decode(action.response.responseText);
						if (json.success == "false"){
							Ext.Msg.alert("Adding Error!!!");
						} else if (json.success == "true"){
							window.close();
							Ext.Msg.alert("Adding succeeded");
							Ext.getCmp("tree").getStore().load();
						}
					},
					failure: function(form, action) {
						Ext.Msg.alert("panel.subimt() - Adding Error!");
					}
				});
		}
		
		var window = Ext.create("Ext.Window", {
			id: "addWindow",
			items: panel,
			width:300,
			buttons: [{
				text: "OK",
				id: "buttonPerformAdd",
				handler: handleOK,
				disabled: true
			}, {
				text: "Cancel",
				handler: function(){
					window.close();
				}
			}],
			title: wtitle
		});
		window.show();	
	} else {
		Ext.Msg.alert("ShowAddWindow - error!!!");
	}

};

var createAddCompanyPanel = function(){
	return Ext.create("Ext.form.FormPanel", {
		items: [{
			xtype: "myfield",
			type: "add",
			name: "name",
			fieldLabel: "Name",
			allowBlank: false
		}, {
			xtype: "textfield",
			name: "address",
			fieldLabel: "Address",
		}, {
			xtype: "textfield",
			name: "phone",
			fieldLabel: "Phone Number",
		}],
		width: 300
	});
}

var createAddDivisionPanel = function(){
	return Ext.create("Ext.form.FormPanel", {
		items: [{
			xtype: "myfield",
			type: "add",
			name: "name",
			fieldLabel: "Name",
			allowBlank: false
		},
		{
			xtype: "textfield",
			name: "phone",
			fieldLabel: "Phone Number"
		}],
		width: 300
	});
}

var createAddProjectPanel = function(){
	return Ext.create("Ext.form.FormPanel", {
		items:[{
			xtype: "myfield",
			type: "add",
			name: "name",
			fieldLabel: "Name",
			allowBlank: false
		}, {
			xtype: "myfield",
			type: "add",
			name: "customer",
			fieldLabel: "Customer",
			allowBlank: false
		}, {
			xtype: "datefield",
			type: "add",
			name: "deadline",
			fieldLabel: "Deadline",
			allowBlank: false,
			value: new Date(),
			minValue: new Date()
		}],
		width: 300
	});
}

var createAddEmployeePanel = function(){
	return Ext.create("Ext.form.FormPanel", {
		items: [{
			xtype: "myfield",
			type: "add",
			name: "name",
			fieldLabel: "Name",
			allowBlank: false,
			type: "add"
		}, {
			xtype: "datefield",
			name: "hired",
			fieldLabel: "Hire date",
			value: new Date(),
			minValue: new Date()
		}, {
			xtype: "textfield",
			name: "skills",
			fieldLabel: "Skills"
		}, {
			xtype: "textfield",
			name: "phone",
			fieldLabel: "Phone Number"
		}, {
			xtype: "myfield",
			type: "add",
			name: "email",
			fieldLabel: "Email"
		}],
		width: 300
	});
}

var performRemoval = function(node){
	Ext.getCmp("tree").el.mask("Processing request", "x-mask-loading");
	Ext.Ajax.request({
		url: "/MyCompany/app/remove",
		params: {
			node: node[0].data.id
		},
		success: function(response, options) {
			Ext.Msg.alert("Removal succeeded");
			Ext.getCmp("tree").el.unmask();
			Ext.getCmp("tree").getStore().load();
		},
		failure: function(xhr){
			Ext.getCmp("tree").el.unmask();
			Ext.Msg.alert("Removal failed!!!");
		}
	});
}

/*****************************************************************************/
/*Details*********************************************************************/
/*****************************************************************************/

var showDetails = function(id){
	Ext.Ajax.request({
		url: "/MyCompany/app/details",
		params: {
			nodeid: id
		},
		success: handleSuccessDetails,
		failure: function(xhr){
			Ext.Msg.alert("Can't get details!!!");
		}
	});
}
var handleSuccessDetails = function(response, options){
	var json = Ext.decode(response.responseText);
	var panel;
	if (json.id.indexOf("company") != -1){
		panel = createCompanyDetailsPanel(json);
	} else if (json.id.indexOf("division") != -1){
		panel = createDivisionDetailsPanel(json);
	} else if (json.id.indexOf("project") != -1){
		panel = createProjectDetailsPanel(json);
	} else if (json.id.indexOf("employee") != -1){
		panel = createEmployeeDetailsPanel(json);
	} else {
		Ext.Msg.alert("Details failed!!!");
	}
	if (panel != undefined){
		var detailsView = Ext.getCmp("details");
		detailsView.removeAll();
		detailsView.add(panel);
		detailsView.doLayout();
	}
}

var createCompanyDetailsPanel = function(json){
	return Ext.create("My.Company.Panel", {
		id: "companyDetailsPanel",
		nodeid: json.id,
		frame: true,
		title: "Company details",
		extraItems: [{
			xtype: "myfield",
			type: "edit",
			name: "name",
			fieldLabel: "Name",
			value: json.text,
			allowBlank: false
		}, {
			xtype: "myfield",
			type: "edit",
			name: "address",
			fieldLabel: "Address",
			value: json.address
		}, {
			xtype: "myfield",
			type: "edit",
			name: "phone",
			fieldLabel: "Phone Number",
			value: json.phone
		}]
	});
}

var createDivisionDetailsPanel = function(json){
	return Ext.create("My.Company.Panel", {
		id: "divisionDetailsPanel",
		nodeid: json.id,
		frame: true,
		title: "Division details",
		extraItems:[{
			xtype: "myfield",
			type: "edit",
			name: "name",
			fieldLabel: "Name",
			value: json.text,
			allowBlank: false
		}, {
			xtype: "myfield",
			type: "edit",
			name: "phone",
			fieldLabel: "Phone Number",
			value: json.phone
		}]
	});
}

var createProjectDetailsPanel = function(json){
	return Ext.create("My.Company.Panel", {
		id: "projectDetailsPanel",
		nodeid: json.id,
		frame: true,
		title: "Project details",
		extraItems: [{
			xtype: "myfield",
			type: "edit",
			name: "name",
			fieldLabel: "Name",
			value: json.text,
			allowBlank: false
		}, {
			xtype: "myfield",
			type: "edit",
			name: "customer",
			fieldLabel: "Customer",
			value: json.customer,
			allowBlank: false
		}, {
			xtype: "datefield",
			type: "edit",
			name: "deadline",
			fieldLabel: "Deadline",
			value: json.deadline,
			minValue: new Date(),
			allowBlank: false,
			listeners: {
				"change": function(sender, newValue, oldValue){
					Ext.getCmp("submitButton").enable();
				}
			}
		}]
	});
}

var createEmployeeDetailsPanel = function(json){
	return Ext.create("My.Company.Panel", {
		id: "employeeDetailsPanel",
		nodeid: json.id,
		frame: true,
		title: "Employee details",
		extraItems:[{
			xtype: "myfield",
			type: "edit",
			name: "name",
			fieldLabel: "Name",
			value: json.text
		}, {
			xtype: "myfield",
			type: "edit",
			name: "hired",
			fieldLabel: "Hired",
			value: json.hired,
			disabled: true
		}, {
			xtype: "myfield",
			type: "edit",
			name: "skills",
			fieldLabel: "Skills",
			value: json.skills
		}, {
			xtype: "myfield",
			type: "edit",
			name: "email",
			fieldLabel: "Email",
			value: json.email
		}, {
			xtype: "myfield",
			type: "edit",
			name: "phone",
			fieldLabel: "Phone Number",
			value: json.phone
		}, {
			xtype: "combo",
			fieldLabel: "Project",
			name: "project",
			displayField: "text",
			store: {
				proxy: {
					type: "ajax",
					url: "/MyCompany/project/listall"
				},
				fields: ["text", "id"]
			},
			value: json.project,
			listeners: {
				"change": function(){
					Ext.getCmp("submitButton").enable();
				}
			}
		}]
	});
}

var submitChanges = function(view, record, node){
	var parameters = {};
	for (i in view.ownerCt.items.items){
		var item = view.ownerCt.items.items[i];
		if (item.xtype != "button"){
			if (item.lastValue != item.originalValue){
				parameters[item.id] = item.lastValue;
			}
		}
	}
	parameters["nodeid"] = view.ownerCt.nodeid;
	var form = Ext.getCmp(view.ownerCt.id);
	if (form){
		form.submit({
			url: "/MyCompany/app/edit",
			params: parameters,
			waitTitle: "Wait",
			waitMsg: "Performing operation..",
			success: function(form, action) {
				var json = Ext.decode(action.response.responseText);
				if (json.success == "true"){
					window.close();
					Ext.Msg.alert("Edit succeeded");
					Ext.getCmp("tree").getStore().load();
					Ext.getCmp("submitButton").disable();
				} else {
					Ext.Msg.alert("Edit failed!");
				}
			},
			failure: function(form, action) {
				Ext.Msg.alert("form.submit() - Edit Error!");
			}
		});
	}
}